package com.zj.jdbc1Tools;

import com.zj.bean.UserInfo;
import com.zj.jdbc1Tools.tools.BeanHandler;
import com.zj.jdbc1Tools.tools.BeanListHandler;
import com.zj.jdbc1Tools.tools.JdbcExecutor;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl {

    public void add(UserInfo user) throws SQLException{
        String sql = "insert into user_inf(name,sex,phoneNumber,birthday) values(?,?,?,?)";
        Object params[] = {user.getName(),user.getSex(),user.getPhoneNumber(),user.getBirthday()};
        JdbcExecutor.update(sql, params);
    }


    public void delete(int id) throws SQLException{
        String sql = "delete from user_inf where id=?";
        Object params[] = {id};
        JdbcExecutor.update(sql, params);
    }

    public void update(UserInfo user) throws SQLException{

        String sql = "update user_inf set name=?,birthday=? where id=?";
        Object params[] = {user.getName(),user.getBirthday(),user.getId()};
        JdbcExecutor.update(sql, params);

    }

    public UserInfo find(int id) throws SQLException{
        String sql = "select * from user_inf where id=?";
        Object params[] = {id};
        return (UserInfo) JdbcExecutor.query(sql, params, new BeanHandler(UserInfo.class));
    }

    public List<UserInfo> getAll() throws SQLException{
        String sql = "select * from user_inf";
        Object params[] = {};
        return (List<UserInfo>) JdbcExecutor.query(sql, params,new BeanListHandler(UserInfo.class));
    }
}